import { createApp } from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import Antd, {notification} from 'ant-design-vue';
import "ant-design-vue/dist/reset.css";
import * as Icons from '@ant-design/icons-vue';
import axios from 'axios';

const app = createApp(App);
// 全局使用图标
const icons = Icons;
for (const i in icons) {
    app.component(i, icons[i]);
}

app.use(store).use(router).use(Antd).mount('#app')
/**
 * axios拦截器 请求时带上token，返回时校验返回码是不是401
 */
axios.interceptors.request.use(function (config) {
    const _token = store.state.member.token;
    if (_token) {
        config.headers.token = _token;
    }
    return config;
}, error => {
    return Promise.reject(error);
});
axios.interceptors.response.use(function (response) {
    return response;
}, error => {
    const response = error.response;
    const status = response.status;
    if (status === 401) {
        // 判断状态码是401 跳转到登录页
        store.commit("setMember", {});
        notification.error({ description: "未登录或登录超时" });
        router.push('/login');
    }
    return Promise.reject(error);
});
axios.defaults.baseURL = process.env.VUE_APP_SERVER;
